<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2018/11/13
 * Time: 11:08
 */

namespace common\logics\upload;

use common\widgets\Tool;
use yii\base\BaseObject;
use Yii;
use common\services\QiniuService;
use common\services\BaiduService;

class UploadLogic extends BaseObject implements IUpload
{


    public function init(){
        parent::init();
    }

    public function uploadChangeImage($upload_img_type)
    {
        if(empty($_FILES['file']['name'])){
            return Tool::returnJson([],401,'缺少图片文件');
        }
        //上传图片到压缩目录下
        $upfile = Yii::$app->params['upload_file_dir'];
        $arr = explode('.', $_FILES['file']['name']);
        //获取图片类型
        if(!in_array(($arr[count($arr)-1]),array('jpg','png','jpeg','gif','JPG','PNG')))
        {
            return Tool::returnJson([],402,'不支持该图片类型');
        }
        //看看配置图片是否存在
        if(empty(Yii::$app->params['upload_images_types_dir'][$upload_img_type])){
            return Tool::returnJson([],406,'该上传类型尚未配置');
        }

        //1. 先将图片存储在临时文件夹中，然后再post 到百度云BOS 图片文件目录中
        $filename = time().rand(1000,9999).".".$arr[count($arr)-1];
        $uploadFiles = $upfile.$filename;
        $tmp_filename = $_FILES['file']['tmp_name'];
        //上传压缩图片
        list($width, $height, $type, $attr) = getimagesize($tmp_filename);
        $size = filesize($tmp_filename);
        //图片大于1024k 进行压缩
        if(($kb = ($size/1024))> 1024){
            //定义压缩率
            if($kb > 2048){
                $percent = 0.6;
            }elseif($kb >1024){
                $percent = 0.8;
            }else{
                $percent = 1;
            }
            $type = image_type_to_extension($type, false);

            $fun = 'imagecreatefrom'. $type;
            $src_image = $fun($tmp_filename);
            $new_width = $width *  $percent;
            $new_height = $height * $percent;
            $image_thump = imagecreatetruecolor($new_width, $new_height);
            //将原图复制带图片的载体上面，并且按照一定的比例压缩
            imagecopyresampled($image_thump, $src_image, 0, 0,0, 0, $new_width, $new_height, $width, $height);
            imagedestroy($src_image);
            $funcs = 'image'. $type;
            $new_image_name = $upfile .time() . uniqid() . '.' .$arr[1];
            $funcs($image_thump, $new_image_name);
            imagedestroy($image_thump);

            $files = $new_image_name;
        }else{
            $files = $tmp_filename;
        }
        try{
            $res = BaiduService::uploadImages($files, $upload_img_type);
            if(!empty($res['url'])){
                return Tool::returnJson($res,200,'上传图片成功');
            }
            return Tool::returnJson([],405,'上传图片失败');
        }catch (\Exception $e){
            return Tool::returnJson([],407,'上传图片异常');
        }
    }

    public function uploadImg($upload_img_type)
    {
        if(empty($_FILES['file']['name'])){
            return Tool::returnJson([],401,'缺少图片文件');
        }
        $arr = explode('.', $_FILES['file']['name']);
        //获取图片类型
        if(!in_array($arr[count($arr)-1],array('jpg','png','jpeg','gif','JPG','PNG')))
        {
            return Tool::returnJson([],402,'不支持该图片类型');
        }
        //看看配置图片是否存在
        if(empty(Yii::$app->params['upload_images_types_dir'][$upload_img_type])){
            return Tool::returnJson([],406,'该上传类型尚未配置');
        }
        try{
            //上传图片操作
            $res = BaiduService::uploadImageToBaidu($upload_img_type);
            if(!empty($res['url'])){
                return Tool::returnJson($res,200,'上传图片成功');
            }
            return Tool::returnJson([],402,'上传图片失败');
        }catch (\Exception $e){
            return Tool::returnJson([],403,'上传图片失败');
        }
    }
}